package com.dingwei.lesyndrome.mvp.presenter;

import android.app.AlertDialog;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.base.FailsBean;
import com.dingwei.lesyndrome.base.MyBaseBean;
import com.dingwei.lesyndrome.bean.IdCodeBean;
import com.dingwei.lesyndrome.custom.myCallback;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.RegexUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class ForgetPasswordPresenter extends basePresenter {

    private Contract.ForgetPasswordView forgetPasswordView;
    private AlertDialog dlg;
    private String picCode = "";
    RequestOptions options;

    public ForgetPasswordPresenter(Contract.ForgetPasswordView view) {
        super(view);
        this.forgetPasswordView = view;
    }


    public void getImgCode(final String phone) {

        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }
        if (!RegexUtils.isMobileExact(phone)) {
            showToast("请输入正确的手机号码");
            return;
        }
        options = new RequestOptions()
                .centerCrop()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE);
        AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.context);
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_code, null);
        builder.setView(view);

        final EditText et_code = (EditText) view.findViewById(R.id.et_code);
        final ImageView img = (ImageView) view.findViewById(R.id.img);
        Button btnCancle = (Button) view.findViewById(R.id.btn_cancel);
        Button btnOk = (Button) view.findViewById(R.id.btn_ok);

        Glide.with(BaseActivity.context)
                .load(URLS.HOST + URLS.IMG_CODE + "?cate=2" + "&phone=" + phone)
                .apply(options)
                .into(img);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(BaseActivity.context)
                        .load(URLS.HOST + URLS.IMG_CODE + "?cate=2" + "&phone=" + phone)
                        .apply(options)
                        .into(img);
            }
        });
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_code.getText().toString())) {
                    showToast("请输入验证码");

                } else {
                    picCode = et_code.getText().toString();
                    dlg.dismiss();
                    getCode(phone);
                }
            }
        });
        dlg = builder.create();
        dlg.show();
    }

    private void getCode(String phone) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.ID_CODE);
        rp.addBodyParameter("imgcode", picCode);
        rp.addBodyParameter("phone", phone);
        rp.addBodyParameter("cate", "2");

        x.http().post(rp, new myCallback(BaseActivity.context, IdCodeBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                IdCodeBean bean = (IdCodeBean) baseBean;
                showToast("验证码已发送");
                forgetPasswordView.getCodeSuccess(bean);
                MyCount myCount = new MyCount(60000, 1000);
                myCount.start();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void forgetPassword(String phone, String idcode, String password, String suerpass) {

        if (TextUtils.isEmpty(phone)) {
            showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileExact(phone)) {
            showToast("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(idcode)) {
            showToast("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入新密码");
            return;
        }
        if (TextUtils.isEmpty(suerpass)) {
            showToast("请确认新密码");
            return;
        }

        RequestParams rp = new RequestParams(URLS.HOST + URLS.FORGET_PASSWORD);
        rp.addBodyParameter("phone", phone);
        rp.addBodyParameter("idcode", idcode);
        rp.addBodyParameter("password", password);
        rp.addBodyParameter("suerpass", suerpass);
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                forgetPasswordView.forgetPasswordSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }


    /**
     * 倒计时
     */
    public class MyCount extends CountDownTimer {


        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            forgetPasswordView.CountDownTimerFinish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            forgetPasswordView.CountDownTimerOnTick(millisUntilFinished);
        }

    }

}
